package com.lsf.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.lsf.common.api.vo.Result;
import com.lsf.modules.system.entity.Comment;
import com.lsf.modules.system.entity.Reply;
import com.lsf.modules.system.entity.User;
import com.lsf.modules.system.service.CommentService;
import com.lsf.modules.system.service.ReplyService;
import com.lsf.modules.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Alexander
 */
@RestController
@Slf4j
@RequestMapping("/sys/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @Resource
    private ReplyService replyService;

    @Resource
    private UserService userService;

    @RequestMapping("/add")
    public Result add(@RequestBody @Validated Comment comment) {
        Result result = new Result<>();
        Reply reply = replyService.getById(comment.getArticleId());
        if (reply == null) {
            result.error500("回帖不存在!");
            return result;
        }
        User user = userService.getById(comment.getAuthorId());
        if (user == null) {
            result.error500("用户不存在!");
            return result;
        }
        comment.setCreateTime(new Date());
        boolean save = commentService.save(comment);
        if (!save) {
            result.error500("添加失败");
        }
        result.success("添加成功");
        return result;
    }
}
